package com.imooc.mymall.domain.common;

import com.imooc.mymall.exception.ErrorCodeEnum;
import com.sun.org.apache.xpath.internal.operations.Bool;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * <h1>通用返回结果模型</h1>
 * @Author: crowsjian
 * @Date: 2020/7/11 14:05
 */
@Data
@ApiModel(
        value = "统一返回结果实体",
        description = "封装统一返回实体结果信息实体"
)
public class ResponseResult<T> implements Serializable {

    private static final long serialVersionUID = -2253119138085315777L;

    /*是否成功*/
    @ApiModelProperty(
            name = "success",
            value = "是否成功",
            required = true,
            dataType = "Boolean"
    )
    private Boolean success;
    /*编码*/
    @ApiModelProperty(
            name = "code",
            value = "编码",
            required = false,
            dataType = "String"
    )
    private String code;
    /*描述信息*/
    @ApiModelProperty(
            value = "描述信息"
    )
    private String message;
    /*返回结果*/
    @ApiModelProperty(
            value = "泛型结果"
    )
    private T result;

    /**
     * <h2>成功</h2>
     * @param result
     * @param <T>
     * @return
     */
    public static <T> ResponseResult<T> success(T result){
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.setSuccess(Boolean.TRUE);
        responseResult.setResult(result);
        return responseResult;
    }


    /**
     * <h2>失败</h2>
     * @param code
     * @param message
     * @param <T>
     * @return
     */
    public static <T> ResponseResult<T> failure(String code,String message){
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.success(Boolean.FALSE);
        responseResult.setCode(code);
        responseResult.setMessage(message);
        return responseResult;
    }

    /**
     * <h2>失败</h2>
     * @param errorCodeEnum
     * @param <T>
     * @return
     */
    public static <T> ResponseResult<T> failure(ErrorCodeEnum errorCodeEnum){
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.success(Boolean.FALSE);
        responseResult.setCode(errorCodeEnum.getCode());
        responseResult.setMessage(errorCodeEnum.getMessage());
        return responseResult;
    }
}
